//
//  FindViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "FindViewController.h"
#import "FindTableViewCell.h"
#import "findDetailViewController.h"

#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
#define sectionHeight 40

@interface FindViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,retain)NSMutableArray *sectionArray;
@property(nonatomic,retain)NSMutableArray *myArray;
@property(nonatomic,retain)NSMutableArray *carouseArray;

@end

@implementation FindViewController

-(void)dealloc{

    [_myTableView release];
    [_sectionArray release];
    [_myArray release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ToDetail" object:nil];
    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //获取数据
    [self dataHandle];
    
    self.myArray = [NSMutableArray array];
    self.sectionArray = [NSMutableArray array];
    self.carouseArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [_myTableView release];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    //导航栏上加一个label显示@"喜马拉雅FM"
    UILabel *NALabel = [[UILabel alloc] initWithFrame:CGRectMake(10,5, 100, 30)];
    NALabel.backgroundColor = [UIColor clearColor];
    NALabel.text = @"边走边听FM";
    NALabel.textColor = [UIColor blueColor];
    [self.navigationController.navigationBar addSubview:NALabel];
    //跳转到详情界面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Action:) name:@"ToDetail" object:nil];
    
    // Do any additional setup after loading the view.
}

-(void)Action:(NSNotification *)noti{

    findDetailViewController *findVc = [[findDetailViewController alloc] init];
    findVc.albumid = noti.object;
    UINavigationController *findNA = [[UINavigationController alloc] initWithRootViewController:findVc];
    
    [self presentViewController:findNA animated:YES completion:^{
        
        
    }];
    [findNA release];
    [findVc release];

}
//数据解析
-(void)dataHandle{

    [ZYNetworkHandle GetDataWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.26" dic:nil success:^(id responseObject) {
        
        
      BigModel *bigmodel = [BigModel yy_modelWithDictionary:responseObject];
       // NSLog(@"%@",bigmodel.hotRecommends.list);
        for (focusListModel *model in bigmodel.focusImages.list) {
            [self.carouseArray addObject:model.pic];
        }
        //section名字的数组
        [self.sectionArray addObjectsFromArray:bigmodel.hotRecommends.list];
        //NSLog(@"%lu",(unsigned long)self.sectionArray.count);
        for (listModel *model in bigmodel.hotRecommends.list) {
           //每个section的row的数组
            [self.myArray addObject:model.list];

        }
        //创建轮播图
        [self createCarouse];
        
        [self.myTableView reloadData];
        
       // NSLog(@"%@",responseObject);
    } filed:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}
//创建轮播图
-(void)createCarouse{

    MyScrollView *myScorll = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 4)];
    [self.view addSubview:myScorll];
    [myScorll setImages:self.carouseArray];
    self.myTableView.tableHeaderView = myScorll;


}
//设置分区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return sectionHeight;

}
//重写分区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, sectionHeight)];
    headerView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:headerView];
   
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.height, headerView.frame.size.height)];
    imageV.backgroundColor = [UIColor clearColor];
    [headerView addSubview:imageV];
    imageV.image = [UIImage imageNamed:@"tabbar_np_play"];
    [imageV release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(headerView.frame.size.height, 0, WIDTH - headerView.frame.size.height, headerView.frame.size.height)];
    titleLabel.backgroundColor = [UIColor clearColor];
    [headerView addSubview:titleLabel];
    listModel *model  = [self.sectionArray objectAtIndex:section];
    titleLabel.text = model.title;
    [titleLabel release];
     [headerView release];
    return headerView;
    
}
//section个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.sectionArray.count;

}
//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return HEIGHT / 3.5;

}
//显示cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"cellReuse";
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[FindTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        
    }
    listModel *model = [self.sectionArray objectAtIndex:indexPath.section];
    cell.model = model;
    cell.backgroundColor = [UIColor lightGrayColor];
    return cell;
    
}
//cell行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}
//取消分区头吸附效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.myTableView)
    {
        CGFloat sectionHeaderHeight = 60; //sectionHeaderHeight
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
